/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "los_ipclimit_pri.h"

ProcIPCLimiter g_procIPCLimiter;

ProcIPCLimiter g_procIPCInfo[LOSCFG_BASE_CORE_PROCESS_LIMIT] = { 0 };

void IPCLimiterInit(void)
{
    return;
}

void *IPCLimiterAlloc(void)
{
    ProcIPCLimiter *plimiter = (ProcIPCLimiter *)LOS_KernelMalloc(sizeof(ProcIPCLimiter));
    if (plimiter == NULL) {
        return NULL;
    }
    memset_s(plimiter, sizeof(ProcIPCLimiter), 0, sizeof(ProcIPCLimiter));
    return (void *)plimiter;
}

void IPCLimiterCopy(UINT8 *dest, UINT8 *src)
{
    return;
}

int IPCLimiterUpdate(UINT32 value, enum IPCStatType statType)
{
    UINT32 intSave;
    LosTaskCB *runTask = OsCurrTaskGet();
    if (g_isInitProcLimiterSet == FALSE) {
        return LOS_OK;
    }
    PLIMITS_LOCK(intSave);
    ProcLimiterSet *procLimiterSet = GetCurrProcLimiterSet(runTask);
    ProcLimiter *procLimiter = procLimiterSet->procLimiterList[PROCESS_LIMITER_ID_IPC];
    UINT32 *statArr = (UINT32 *)procLimiter->private + (enum IPCStatOffset)statType;
    UINT32 *taskStatArr = (UINT32 *)(&g_procIPCInfo[runTask->processID]) + (enum IPCStatOffset)statType;

    if ((statArr[STAT_ITEM_LIMIT] > 0) && (value > 0)) {
        if ((statArr[STAT_ITEM_TOTAL] + value) > statArr[STAT_ITEM_LIMIT]) {
            taskStatArr[STAT_ITEM_FAILED]++;
			statArr[STAT_ITEM_FAILED]++;
            PLIMITS_UNLOCK(intSave);
            return LOS_NOK;
        }
    }
    taskStatArr[STAT_ITEM_TOTAL] += value;
    do {
        statArr[STAT_ITEM_TOTAL] += value;
        procLimiter = procLimiter->parent;
        statArr = (procLimiter == NULL) ? NULL : (UINT32 *)procLimiter->private + (enum IPCStatOffset)statType;
    } while (statArr != NULL);
    PLIMITS_UNLOCK(intSave);
    return LOS_OK;
}

int IPCLimiterShowStat(struct SeqBuf *seqBuf, void *data)
{
    UINT32 intSave;
    PLIMITS_LOCK(intSave);
    ProcLimiterSet *plimits = (ProcLimiterSet *)data;
    ProcIPCLimiter *plimiter = (ProcIPCLimiter *)plimits->procLimiterList[PROCESS_LIMITER_ID_IPC]->private;
    (void)LosBufPrintf(seqBuf, "mq success count: %u\n", plimiter->mqCount);
    (void)LosBufPrintf(seqBuf, "mq failed count: %u\n", plimiter->mqFailedCount);
    (void)LosBufPrintf(seqBuf, "shm success size: %u\n", plimiter->shmSize);
    (void)LosBufPrintf(seqBuf, "shm failed count: %u\n", plimiter->shmFailedCount);
    PLIMITS_UNLOCK(intSave);
    return LOS_OK;
}

void IPCLimiterMigrate(void)
{
    UINT64 updateTime = OsGetCurrSchedTimeCycle();

    for (int pid = 0; pid < LOSCFG_BASE_CORE_PROCESS_LIMIT; ++pid) {
        if ((g_procLimiterSetArray[pid] == NULL) ||
            (g_procLimiterSetArray[pid]->procLimiterList[PROCESS_LIMITER_ID_IPC] == NULL) ||
            (g_procLimiterSetArray[pid]->procLimiterList[PROCESS_LIMITER_ID_IPC]->private == NULL)) {
            continue;
        }
        ProcIPCLimiter *procIPCLimiter = 
            (ProcIPCLimiter *)g_procLimiterSetArray[pid]->procLimiterList[PROCESS_LIMITER_ID_IPC]->private;
		if (procIPCLimiter->migrateTime < updateTime) {
            procIPCLimiter->mqCount = 0;
            procIPCLimiter->mqFailedCount = 0;
            procIPCLimiter->shmSize = 0;
            procIPCLimiter->shmFailedCount = 0;
        }
        procIPCLimiter->mqCount += g_procIPCInfo[pid].mqCount;
        procIPCLimiter->mqFailedCount += g_procIPCInfo[pid].mqFailedCount;
        procIPCLimiter->shmSize += g_procIPCInfo[pid].shmSize;
        procIPCLimiter->shmFailedCount += g_procIPCInfo[pid].shmFailedCount;
		
    }
    return;
}
